package inventoryservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type InventoryAssetListAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewInventoryAssetListAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InventoryAssetListAddLogic {
	return &InventoryAssetListAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// InventoryAssetListAdd 创建资产入库清单表
func (l *InventoryAssetListAddLogic) InventoryAssetListAdd(in *property_service.InventoryAssetList) (*property_service.Response, error) {
	// 获取资产入库清单表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).InventoryAssetList
	// 创建一个新的资产入库清单表模型对象，不包含ID、UpdatedAt、CreatedAt字段
	mod := &model.InventoryAssetList{
		AssetID:           in.AssetID,                                 // 资产ID
		ReceiptID:         in.ReceiptID,                               // 入库单ID
		AssetName:         in.AssetName,                               // 资产名称
		AssetCategory:     in.AssetCategory,                           // 资产类别
		AssetCategoryID:   in.AssetCategoryId,                         //资产类别ID
		Uint:              in.Unit,                                    //单位
		AssetCode:         in.AssetCode,                               // 资产编号
		SpecModel:         in.SpecModel,                               // 规格型号
		UnitPrice:         in.UnitPrice,                               // 单价
		Quantity:          in.Quantity,                                // 数量
		OriginalValue:     in.OriginalValue,                           // 资产原值
		InstallationCost:  in.InstallationCost,                        // 安装费用
		MiscellaneousFees: in.MiscellaneousFees,                       // 杂运费
		Tax:               in.Tax,                                     // 税费
		FundingSource:     in.FundingSource,                           // 资金来源
		IsAccessory:       in.IsAccessory,                             // 是否附属设备
		Manufacturer:      in.Manufacturer,                            // 生产厂商
		AcceptanceDate:    BeanUtils.GetDateFormat(in.AcceptanceDate), // 验收日期
		AllocationInfo:    in.AllocationInfo,                          // 领用信息
		Remarks:           in.Remarks,                                 // 备注
		Attachment:        in.Attachment,                              // 附件
		StatusID:          in.StatusID,                                // 状态ID
		StatusName:        in.StatusName,                              //状态名称
		TenantID:          in.TenantID,                                // 租户ID
		CreatedAtuser:     in.CreatedAtuser,                           // 创建用户
		PurchaseID:        in.PurchaseID,                              //采购申请单ID
		PurchaseAssetID:   in.PurchaseAssetID,                         //采购资产详细单ID
		PurchaseCode:      in.PurchaseCode,                            //采购单编码
	}

	// 使用上下文执行创建操作
	if err := m.WithContext(l.ctx).Create(mod); err != nil {
		// 如果创建失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 创建成功，返回成功响应
	return result.Suc().AddEd()
}
